package com.kaka.bus.engine.amount.loanproduct;

import com.kaka.bus.bean.BpmList;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.LoanProApplyBean;
import com.kaka.bus.bean.ProductDetailBean;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.common.impl.LoanService;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import java.util.List;

import javax.inject.Inject;

public class LoanProductPresenter extends RxPresenter<LoanProductContract.View> implements LoanProductContract.Presenter {

    private LoanService mLoanService;


    @Inject
    public LoanProductPresenter(DataManager dataManager, LoanService loanService) {
        super(dataManager);
        mLoanService = loanService;
        setService(mLoanService);
    }


    @Override
    public void loadProductDetail(String userid, String token, String tokenKey, String device, int productId) {
        mView.showDialog(Constant.LOAD);
        addSubscribe(mDataManager.getApi()
                .getImmediateApply(userid, token, tokenKey, device, productId).compose(T.D())
                .subscribeWith(new Result<HttpWrapper<LoanProApplyBean>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<LoanProApplyBean> applyBeanHttpWrapper) {
                        mView.getLoadProductDetailSuccess(applyBeanHttpWrapper);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public void getBpmList(String userId, String token, String prodictid) {
        mLoanService.getBpmList(userId, token, prodictid, new Result<HttpWrapper<List<BpmList>>>() {
            @Override
            protected void onSuccess(HttpWrapper<List<BpmList>> listHttpWrapper) {
                if (listHttpWrapper != null && listHttpWrapper.getData() != null) {
                    mView.getBpmListSuccess(listHttpWrapper);
                }
            }
        });
    }

    @Override
    public void getImmediateBtn(String userid, String token, String tokenKey, String device, int productId) {
        addSubscribe(mDataManager
                .getApi()
                .getImmediateBtn(userid, token, tokenKey, device, productId)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<LoanProApplyBean>>() {

                    @Override
                    protected void onSuccess(HttpWrapper<LoanProApplyBean> loanProApplyBeanHttpWrapper) {
                        mView.getLoadImmediateBtnSuccess(loanProApplyBeanHttpWrapper);
                    }
                }));
    }

    @Override
    public void getDrainProduct(String per_id, String token, String device, String version, String tokenKey, String productId) {
        addSubscribe(mDataManager.getApi()
                .getDrainProduct(per_id, token, device, version, tokenKey, productId).compose(T.D())
                .subscribeWith(new Result<HttpWrapper<ProductDetailBean>>() {

                    @Override
                    protected void onSuccess(HttpWrapper<ProductDetailBean> wrapper) {
                        mView.drainProductSuccess(wrapper);

                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        mView.drainProductFail();
                    }
                }));
    }

    @Override
    public void chargeApply(String productId, String bannerId, String perId, String chargeProductId) {

        addSubscribe(mDataManager.getApi().chargeApply("apply", productId, bannerId, Constant.PLATFORM, perId, "1")
                .compose(T.D())
                .subscribeWith(new Result<String>() {
                    @Override
                    protected void onSuccess(String stringHttpWrapper) {

                    }
                })
        );

    }


}
